/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.model;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Vector;

import javax.swing.JComboBox;
import javax.swing.table.AbstractTableModel;

import com.ruipengkj.ApplicationContext;
import com.ruipengkj.bean.TEmployee;
import com.ruipengkj.bean.TShopCharge;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.commons.util.DateUtil;
import com.ruipengkj.commons.util.MatcherFormatUtil;
import com.ruipengkj.service.TEmployeeService;


/*
 * 菜品管理model
 * @Author：黄盛强
 * @Date：2012-06-16
 */
public class MemberFillCardChargeModel extends AbstractTableModel{

	private static final long serialVersionUID = 1L;
	private List<TShopCharge> tShopChargeList=new ArrayList<TShopCharge>();
	private Vector<String> columns = new Vector<String>();
	
	
	public MemberFillCardChargeModel(){
		columns.add("");
		columns.add("选择");
		columns.add("金额");
		columns.add("赠送金额/放大率");
		columns.add("赠送类型");
		columns.add("操作人");
		columns.add("开始时间");
		columns.add("结束时间");
		columns.add("操作时间");
		columns.add("是否有效");
	}
	
	//获得行数
	@Override
	public int getRowCount(){
		return tShopChargeList.size();
	}
	
	//获得列数
	@Override
	public int getColumnCount(){
		return columns.size();
	}
	//获取列名
	@Override
	public String getColumnName(int column){
		return (String)columns.get(column);
	}
	//设置单元格是否可编辑
	@Override
	public boolean isCellEditable(int rowIndex,int columnIndex){
		if (columnIndex == 0 || columnIndex == 5 || columnIndex == 8){
			return false;
		}
		return true;
	}
	//获取列对应的类型
	public Class<?> getColumnClass(int column){
		if(column==1 || column == 9){
			return Boolean.class;
		} 
		if (column == 4 || column == 6 || column == 7) 
			return JComboBox.class;
		return Object.class;
	}
	//获得单元格的值
	@Override
	public Object getValueAt(int row,int column){
		if(tShopChargeList != null){
			TShopCharge tShopCharge = tShopChargeList.get(row);
			switch (column) {
				case 0:
					return "*";
				case 1:
					return tShopCharge.isSelect();
				case 2:
					return tShopCharge.getAMount();
				case 3:
					return tShopCharge.getGiftAmount();
				case 4:
					return tShopCharge.getType();
				case 5:
					return tShopCharge.getOperatorName();
				case 6:
					return tShopCharge.getStartTimeStr();
				case 7:
					return tShopCharge.getEndTimeStr();
				case 8:
					return tShopCharge.getHappenTimeStr();
				case 9:
					return tShopCharge.isIsValid();
			}
		}
		return null;
	}
	
	//设置单元格的值
	@Override
	public void setValueAt(Object aValue,int rowIndex,int columnIndex){
		TShopCharge tShopCharge = tShopChargeList.get(rowIndex);
		if(tShopCharge!=null&&aValue!=null){
			switch (columnIndex) {
				case 0:
					break;
				case 1:
					tShopCharge.setSelect(!tShopCharge.isSelect());
					return;
				case 2:
					if(aValue!=null&&!aValue.toString().equals("")&&MatcherFormatUtil.MatcherPriceScheme(aValue.toString())&&aValue.toString().length()<10){
						tShopCharge.setAMount(Double.parseDouble(aValue.toString()));
					}
					break;
				case 3:
					if(aValue!=null&&!aValue.toString().equals("")&&MatcherFormatUtil.MatcherPriceScheme(aValue.toString())&&aValue.toString().length()<10){
						tShopCharge.setGiftAmount(Double.parseDouble(aValue.toString()));
					}
					break;
				case 4:
					tShopCharge.setType(aValue.toString());
					break;
				case 5:
					return;
				case 6:
					tShopCharge.setStartTimeStr(aValue.toString());
					try {
						tShopCharge.setStartTime(DateUtil.getDate(aValue.toString(), DateUtil.TYPE_DATE));
					} catch (ParseException e) {
						e.printStackTrace();
					}
					break;
				case 7:
					tShopCharge.setEndTimeStr(aValue.toString());
					try {
						tShopCharge.setEndTime(DateUtil.getDate(aValue.toString(), DateUtil.TYPE_DATE));
					} catch (ParseException e) {
						e.printStackTrace();
					}
					break;
				case 9:
					tShopCharge.setIsValid(!tShopCharge.isIsValid());
					break;
			}
			tShopCharge.setSelect(true);
			fireTableCellUpdated(rowIndex, 1);
		}
	}
	
	/**
	 * 删除
	 * */
	public void delTShopCharge(int index) {
		if (tShopChargeList!=null&&tShopChargeList.size() > 0) {
			if (index >= 0 && index < tShopChargeList.size()) {
				tShopChargeList.remove(index);
				fireTableRowsInserted(0, tShopChargeList.size() - 1);
				return;
			}
			tShopChargeList.remove(tShopChargeList.size()-1);
			fireTableRowsInserted(tShopChargeList.size()-1, tShopChargeList.size()-1);
		}
	}
	/**
	 * 修改
	 * */
	public void modifyTShopCharge(int index,TShopCharge tShopCharge) {
		if (tShopChargeList!=null&&tShopChargeList.size() > 0) {
			if (index >= 0 && index < tShopChargeList.size()) {
				tShopChargeList.set(index, tShopCharge);
				fireTableRowsInserted(index, index);
			}
		}
	}
	/**
	 * 返回所有
	 * */
	public List<TShopCharge> findAllTShopChargeList() {
		return tShopChargeList;
	}
	/**
	 * 设置当前list数据
	 * */
	public void setTShopChargeList(List<TShopCharge> tShopChargeList){
		this.tShopChargeList= tShopChargeList;
		TEmployeeService tEmployeeService = BuildSpringApplication.getInstance().getTEmployeeServiceImpl();
		for (int i = tShopChargeList.size() - 1; i >= 0; --i) {
			tShopChargeList.get(i).setStartTimeStr(DateUtil.format(tShopChargeList.get(i).getStartTime(), DateUtil.TYPE_DATE));
			tShopChargeList.get(i).setEndTimeStr(DateUtil.format(tShopChargeList.get(i).getEndTime(), DateUtil.TYPE_DATE));
			tShopChargeList.get(i).setHappenTimeStr(DateUtil.format(tShopChargeList.get(i).getHappenTime(), DateUtil.TYPE_DATETIME));
			
			TEmployee tEmployee = null;
			try {
				tEmployee = tEmployeeService.getNameByIdService(tShopChargeList.get(i).getOperator());
			} catch (Exception e) {
				e.printStackTrace();
			}
			if (tEmployee != null)
				tShopChargeList.get(i).setOperatorName(tEmployee.getName());
		}
	}
	/**
	 * 清空所有
	 * */
	public void clearAllTShopCharge(){
		tShopChargeList.clear();
	}
	/**
	 * 添加一行
	 * */
	public void addRow() {
		TShopCharge tShopCharge = new TShopCharge();
		tShopCharge.setSelect(true);
		tShopCharge.setIsValid(true);
		tShopCharge.setOperatorName(ApplicationContext.OPERATOR_NAME);
		tShopCharge.setOperator(ApplicationContext.OPERATOR_ID);
		tShopCharge.setHappenTime(new Date());
		tShopCharge.setHappenTimeStr(DateUtil.format(tShopCharge.getHappenTime(), DateUtil.TYPE_DATETIME));
		tShopCharge.setType("1、充值xx送xx");
		
		tShopChargeList.add(tShopCharge);
		this.fireTableRowsInserted(tShopChargeList.size()-1, tShopChargeList.size()-1);
	}
}
